local UIBase = require("view.UIBase")
local GMView = class("GMView", UIBase)
local UIUtil = require("utils.UIUtil")
local GameUtils = require("utils.GameUtils")
local type_manage = require("config.type_manage")

function GMView:ctor()
    local resource = {
        path = "Prefabs/View",
        name = "GMView",
        canvas = 2
    }
    GMView.super.ctor(self, resource)
    self._selectedInex = 0
end

function GMView:awake()
    self._listScroll:SetTemplate(
        handler(self, self._onItemUpdate),
        handler(self, self._onValueChange),
        handler(self, self._onItemTouch)
    )
end

function GMView:start()
    self._btnSend.onClick:AddListener(handler(self, self._onBtnSendClick))
    self._btnClose.onClick:AddListener(handler(self, self._onBtnCloseClick))
    self._listScroll:ReSize(type_manage.length())
end

function GMView:_onItemUpdate(item, index)
    local startIndex = index + 1
    local data = type_manage.indexOf(startIndex)
    item:updateUI(data, startIndex, self._selectedInex)
end

function GMView:_onValueChange()
end

function GMView:_onItemTouch(item, index)
    printDebug("GMView:_onItemTouch " .. index)
    self._selectedInex = index
    self._listScroll:UpdateUsedCell()
end

function GMView:_onBtnCloseClick()
    self:close()
end

function GMView:_onBtnSendClick()
    if self._selectedInex > 0 then
        local data = type_manage.indexOf(self._selectedInex)
        local value = tonumber(self._textValue.text)
        local size = tonumber(self._textNums.text)
        if data and value > 0 and size > 0 then
            local param = {}
            param.type = data.id
            param.value = value
            param.size = size
            G_DropManager:drop(param)
            printDebug("GMView:_onBtnSendClick")
        end
    end
end

return GMView
